import axios from "axios";
import { Message } from "element-ui";
import qs from "qs";
import request from "../utils/service";
export function commAxiosRequest(param, iscross) {
  debugger;
  if (param.ajaxType == "post") {
    const formData = new FormData();
    for (let key in param.apiparam.body) {
      if (param.apiparam.body[key] == "" || param.apiparam.body[key] == null) {
        delete param.apiparam.body[key];
      } else {
        formData.append(key, param.apiparam.body[key]);
      }
    }
    if (iscross) {
      return new Promise((resolve, reject) => {
        return axios
          .post(param.apiurl, formData, {
            headers: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
          })
          .catch((error) => {
            Message.error("请求地址报错！" + error);
          })
          .then((res) => {
            if (res.status == 200) {
              resolve(res.data);
            } else {
              Message.error("请求地址报错！" + res.statusText);
            }
          });
      });
    } else {
      return axios
        .post(param.apiurl, formData, {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
        })
        .catch((error) => {
          Message.error("请求地址报错！" + error);
        });
    }
  } else {
    if (iscross) {
      return new Promise((resolve, reject) => {
        axios
          .get(param.apiurl, {
            params: param.apiparam.query,
            headers:
              param.headjson == null || param.headjson == ""
                ? {}
                : JSON.parse(param.headjson),
          })
          .catch((error) => {
            Message.error("请求地址报错！" + error);
          })
          .then((res) => {
            debugger;
            if (res.status == 200) {
              resolve(res.data);
            } else {
              Message.error("请求地址报错！" + res.statusText);
            }
          });
      });
    } else {
      return axios
        .get(param.apiurl, {
          params: param.apiparam.query,
          headers:
            param.headjson == null || param.headjson == ""
              ? {}
              : JSON.parse(param.headjson),
        })
        .catch((error) => {
          Message.error("请求地址报错！" + error);
        });
    }
  }
}
export function ajaxComm(apicode, param) {
  let apiurl = "/ui/common/apicomm/" + apicode;
  if (param.query != null) {
    apiurl = apiurl + "?" + qs.stringify(param.query);
  }
  return request({
    url: apiurl,
    method: "post",
    data: param.body,
  });
}
